package com.team.childapp.ui.contacts

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.huanguo.dashboard.base.adapter.KotlinDataAdapter
import com.mig35.carousellayoutmanager.CarouselLayoutManager
import com.mig35.carousellayoutmanager.CarouselLayoutManager.HORIZONTAL
import com.mig35.carousellayoutmanager.CarouselZoomPostLayoutListener
import com.mig35.carousellayoutmanager.CenterScrollListener
import com.mig35.carousellayoutmanager.DefaultChildSelectionListener
import com.team.childapp.R
import com.team.childapp.base.App
import com.team.childapp.base.activity.BaseActivity
import com.team.childapp.base.listener.ICallBackResultListener
import com.team.childapp.bean.MainItem
import com.team.childapp.bean.constants.ConstantsBean
import com.team.childapp.databinding.ActivityModifyContactsBinding
import com.team.childapp.http.ConstantsLogical
import com.team.childapp.util.ToastUtil
import java.util.Locale

/**
 * Create on: 2024/12/30
 * by Mr.Zhu
 * Description: 添加修改联系人信息
 */
class ModifyContactsActivity: BaseActivity<ActivityModifyContactsBinding>(R.layout.activity_modify_contacts) {
    private val list = arrayListOf<Int>()
    private var currentHead:Int = 0
    private var id:String = ""
    override fun initData() {
        generateHeadList()
        val serializableExtra = intent.getSerializableExtra("contact")
        if (serializableExtra != null) {
            val contact = serializableExtra as ConstantsBean
            setDefaultData(contact)
        }

        mBinding.modifyImageBack.setOnClickListener {
            finish()
        }

        mBinding.layoutSure.setOnClickListener {
            val nickName = mBinding.etNickName.text.toString()
            if (TextUtils.isEmpty(nickName)) {
                Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val phone = mBinding.etPhone.text.toString()
            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(this, "请输入电话", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(id)) {//新增
                ConstantsLogical.getInstanceLogin().requestAddContacts(this,nickName,phone,currentHead,object :
                    ICallBackResultListener {
                    override fun onCallBack(result: Any?) {
                        if (result is Boolean) {
                            success()
                        }
                    }
                })
            } else{//更新
                ConstantsLogical.getInstanceLogin().requestUpdateContacts(this,nickName,phone,currentHead,id,object :
                    ICallBackResultListener {
                    override fun onCallBack(result: Any?) {
                        if (result is Boolean) {
                            success()
                        }
                    }
                })
            }
        }
        initAdapter()
        mBinding.imageviewHead.setOnClickListener {
            mBinding.layoutInoutNick.visibility = View.GONE
            mBinding.layoutInoutPhone.visibility = View.GONE
            mBinding.layoutSure.visibility = View.GONE
            mBinding.modifyImageBack.visibility = View.GONE
            mBinding.headRecycler.visibility = View.VISIBLE
        }
    }

    private fun initAdapter(){
        val layoutManager = GridLayoutManager(this, 4).apply {
        } // 每行显示 4 个 Item
        mBinding.headRecycler.apply {
            this.layoutManager = layoutManager
            adapter = KotlinDataAdapter.Builder<Int>()
                .setLayoutId(R.layout.adapter_contants_head)
                .setData(list)
                .addBindView { itemView, itemData, position ->
                    itemView.findViewById<ImageView>(R.id.imageview_head)?.let { imageView ->
                        itemData?.let { avatarUrl ->
                            Glide.with(itemView.context)
                                .load(avatarUrl)
                                .into(imageView)
                        }
                    }
                    itemView.rootView.setOnClickListener({
                        setCurrentHead(itemData)
                    })
                }
                .create()
            }
    }

    private fun setCurrentHead(resourceId :Int) {
        Glide.with(this)
            .load(resourceId)
            .into(mBinding.imageviewHead)
        currentHead = list.indexOf(resourceId)
        if (mBinding.headRecycler.visibility == View.VISIBLE) {
            mBinding.headRecycler.visibility = View.GONE
            mBinding.layoutInoutNick.visibility = View.VISIBLE
            mBinding.layoutInoutPhone.visibility = View.VISIBLE
            mBinding.layoutSure.visibility = View.VISIBLE
            mBinding.modifyImageBack.visibility = View.VISIBLE
        }
    }

    private fun generateHeadList(){
        list.add(R.mipmap.icon_head_baba)
        list.add(R.mipmap.icon_head_mam)
        list.add(R.mipmap.icon_head_gege)
        list.add(R.mipmap.icon_head_jiejie)
        list.add(R.mipmap.icon_head_yeye1)
        list.add(R.mipmap.icon_head_yeye2)
        list.add(R.mipmap.icon_head_nainia1)
        list.add(R.mipmap.icon_head_nainai2)
    }

    private fun setDefaultData(bean: ConstantsBean){
        if (bean != null) {
            id = bean.id
            mBinding.etNickName.setText(bean.name)
            mBinding.etPhone.setText(bean.telephone)
            var it = bean.avatar
            var resource= R.mipmap.icon_head_baba//爸爸
            if (TextUtils.equals(it, "1")) {//妈妈
                resource = R.mipmap.icon_head_mam
            } else if (TextUtils.equals(it, "2")) {//哥哥
                resource = R.mipmap.icon_head_gege
            }else if (TextUtils.equals(it, "3")) {//姐姐
                resource = R.mipmap.icon_head_jiejie
            }else if (TextUtils.equals(it, "4")) {//爷爷
                resource = R.mipmap.icon_head_yeye1
            }else if (TextUtils.equals(it, "5")) {
                resource = R.mipmap.icon_head_yeye2
            }else if (TextUtils.equals(it, "6")) {//奶奶
                resource = R.mipmap.icon_head_nainia1
            }else if (TextUtils.equals(it, "7")) {
                resource = R.mipmap.icon_head_nainai2
            }
            mBinding.imageviewHead.setImageResource(resource)
        }
    }

    private fun success(){
        App.application().getHandler().postDelayed({
            ToastUtil.showLong("成功")
            finish()
        },300)
    }

}